package main

import (
	"fmt"
	"gitee.com/baal1990/epidemic/global"
	"gitee.com/baal1990/epidemic/initialize"
	"os"
)

func main() {
	args := os.Args
	if len(args) == 0 || len(args) != 5 {
		fmt.Println("usage: -k public/private")
		fmt.Println("       -e encrypt")
		fmt.Println("       -d decrypt")
		return
	}
	keyType := args[2]
	cryptoMethod := args[3]
	content := args[4]
	initialize.InitAsymmetricCrypto("private.key", "public.key")
	switch cryptoMethod {
	case "-e":
		var result string
		var err error
		if keyType == "private" {
			result, err = global.AsymmetricCrypto.EncryptWithPrvKOutputBase64([]byte(content))
			if err != nil {
				fmt.Println("加密失败", err)
				return
			}
		} else {
			result, err = global.AsymmetricCrypto.EncryptWithPubKeyOutputBase64([]byte(content))
			if err != nil {
				fmt.Println("加密失败", err)
				return
			}
		}
		fmt.Println("[", result, "]")
	case "-d":
		var result []byte
		var err error
		if keyType == "public" {
			result, err = global.AsymmetricCrypto.DecryptWithPubKeyFromBase64(content)
			if err != nil {
				fmt.Println("解密失败", err)
				return
			}
		} else {
			result, err = global.AsymmetricCrypto.DecryptWithPrvKeyFromBase64(content)
			if err != nil {
				fmt.Println("解密失败", err)
				return
			}
		}
		fmt.Println("[", string(result), "]")
	}
}
